

global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area

global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 

clear 

******************************************************************
* Load raw panel data CBHP : 2006-2017 + CREATE BASELINE DATASET
*******************************************************************

use "$data/PE064_CBHP_A_YFRM_20062017_JUN19_ROSTO_V01.dta", clear
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_MG_V01.dta"
drop _m
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_PESSOAL_V01.dta"
drop _m
g export_service_EU=0
replace export_service_EU=1 if MG005!=0
bys ano: tab export_service_EU
bys ano: tab exporta
g export_sales_EU=0
replace export_sales_EU=1 if MG004!=0
bys ano: tab export_sales_EU
preserve
collapse (sum)  export_sales_EU export_service_EU, by(ano)
twoway (connected export_sales_EU ano) (connected export_service_EU ano), ///
graphregion(fcolor(white) lcolor(white)) xlabel(2006 (1) 2017, angle(vertical)) ///
legend(label(1 "Goods") label(2 "Services") ring(0) pos(5) cols(1)) xtitle("") ytitle("Number of Firms")
graph export "$outputs/exports_nb_EU.pdf",replace
restore
preserve 
collapse (sum)  MG005 MG004 export_sales_EU export_service_EU, by(ano)

twoway (connected MG004 ano) (connected MG005 ano), ///
graphregion(fcolor(white) lcolor(white)) xlabel(2006 (1) 2017, angle(vertical)) ///
legend(label(1 "Goods") label(2 "Services") ring(0) pos(11) cols(1)) xtitle("") ytitle("Euros")
graph export "$outputs/exports_amount_EU.pdf",replace

twoway (connected MG005 ano), ///
graphregion(fcolor(white) lcolor(white)) xlabel(2006 (1) 2017, angle(vertical)) ///
 xtitle("") ytitle("Number of Firms")
graph export "$outputs/exports_amount_EU_onlyservice.pdf",replace

restore

tabout cae3 if export_service_EU==1 using $outputs/sectorexport.xls, cells(freq col) replace

g postable=0
foreach y in $list {
replace postable=1 if cae3==`y'
}

preserve 
collapse (sum)  export_service_EU, by(ano postable)

twoway (connected export_service_EU ano if postable==1) , ///
graphregion(fcolor(white) lcolor(white)) xlabel(2006 (1) 2017, angle(vertical)) ///
legend(label(1 "Posting") label(2 "Other Services") ring(0) pos(5) cols(1)) xtitle("") ytitle("Number of Firms")
graph export "$outputs/exports_nb_EU_post.pdf",replace
restore
g flag=1
egen tot=sum(flag), by(ano cae3)

preserve 
collapse (sum)  export_service_EU flag, by(ano postable)
g share_export_services=export_service_EU/flag

twoway (connected share_export_services ano if postable==1), ///
graphregion(fcolor(white) lcolor(white)) xlabel(2006 (1) 2017, angle(vertical)) ///
xtitle("") ytitle("Share of Firms Exporting Services") ylabel(0 (0.06) 0.12)
graph export "$outputs/exports_share_posting.pdf",replace


twoway (connected share_export_services ano if postable==0), ///, ///
graphregion(fcolor(white) lcolor(white)) xlabel(2006 (1) 2017, angle(vertical)) ///
xtitle("") ytitle("Share of Firms Exporting Services") ylabel(0 (0.06) 0.12)
graph export "$outputs/exports_share_other.pdf",replace


restore


egen panel_export=sum(export_service_EU), by(tina)
egen panel=sum(flag), by(tina)
count if postable==1 & panel_export>=1

merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_CONTAS_V01.dta"
drop _m

*Keep sectors where exports of services is through mode IV supply of services 
keep if postable==1
drop panel_export 
egen panel_export=sum(export_service_EU), by(tina)

sum E002 if panel_export==0, d 
sum E002 if panel_export>=1, d 
sum E001 if panel_export==0, d 
sum E001 if panel_export>=1, d 

twoway (hist E001 [] if panel_export==0 & E001<50 , frac fcolor(eltblue) lcolor(white)) ///
(hist E001 [] if panel_export>=1 & E001<50 , frac fcolor(none) lcolor(gs5)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Number of Employees") ///
legend(label(1 "Non Posting Firms") label(2 "Posting Firms")) 
graph export "$outputs/employees_distribution_allpostable.pdf",replace

foreach y in $list{
sum E001 if panel_export>=1 & cae3==`y', d 
global mean=r(p99)
twoway (hist E001 [] if panel_export==0 & E001<$mean & cae3==`y', frac fcolor(eltblue) lcolor(white)) ///
(hist E001 [] if panel_export>=1 & E001<$mean & cae3==`y', frac fcolor(none) lcolor(gs5)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Number of Employees") ///
legend(label(1 "Non Posting Firms") label(2 "Posting Firms")) 
graph export "$outputs/employees_distribution_`y'.pdf",replace

}

foreach y in $list{
sum E012 if panel_export>=1 & cae3==`y', d 
global mean=r(p99)
twoway (hist E012 [] if panel_export==0 & E012<$mean & cae3==`y', frac fcolor(eltblue) lcolor(white)) ///
(hist E012 [] if panel_export>=1 & E012<$mean & cae3==`y', frac fcolor(none) lcolor(gs5)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Number of Hours Worked") ///
legend(label(1 "Non Posting Firms") label(2 "Posting Firms")) 
graph export "$outputs/hours_distribution_`y'.pdf",replace

}

sum E012 if panel_export>=1, d 
global mean=r(p95)

twoway (hist E012 [] if panel_export==0 & E012<$mean , frac fcolor(eltblue) lcolor(white)) ///
(hist E012 [] if panel_export>=1 & E012<$mean , frac fcolor(none) lcolor(gs5)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Number of Hours Worked") ///
legend(label(1 "Non Posting Firms") label(2 "Posting Firms")) 
graph export "$outputs/hours_distribution_allpostable.pdf",replace



g wage_cap=D029/E012
sum wage_cap if panel_export>=1, d 
global mean=r(p99)

twoway (hist wage_cap [] if panel_export==0 & wage_cap<$mean , frac fcolor(eltblue) lcolor(white)) ///
(hist wage_cap [] if panel_export>=1 & wage_cap<$mean , frac fcolor(none) lcolor(gs5)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Average Hourly Wage") ///
legend(label(1 "Non Posting Firms") label(2 "Posting Firms")) 
graph export "$outputs/wagecap_distribution_allpostable.pdf",replace


foreach y in $list{
sum wage_cap if panel_export>=1 & cae3==`y', d 
global mean=r(p99)
twoway (hist wage_cap [] if panel_export==0 & wage_cap<$mean & cae3==`y', frac fcolor(eltblue) lcolor(white)) ///
(hist wage_cap [] if panel_export>=1 & wage_cap<$mean & cae3==`y', frac fcolor(none) lcolor(gs5)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Average Hourly Wage") ///
legend(label(1 "Non Posting Firms") label(2 "Posting Firms")) 
graph export "$outputs/wage_cap_distribution_`y'.pdf",replace

}

*Create event of first posting and corresponding event to time dummies*

forval y=2006/2017{
g dum_`y'=0
replace dum_`y'=1 if ano==`y' & export_service_EU==1
egen dumi_`y'=max(dum_`y'), by(tina)
}


bysort tina (ano):gen cum=sum(export_service_EU)

g event=ano if cum==1 & export_service_EU==1
egen event_d=min(event), by(tina)
replace event_d=0 if missing(event_d)
g treated=0
replace treated=1 if panel_export>=1
g time=ano-event_d
replace time=0 if treated==0 & panel_export==0

su time, d 
su timed, d

*Need to change this part of the code if you change the coverage of the sample (2006-2017 is my baseline)
*instead of using 11 and 22 in the loop
*you can use r(max) of sum time, timed -- see fig1.do for implementation of similar looping. 


g timed=time+11 if treated==1
replace timed=10 if treated==0

forval y=0/22{
g coeff_`y'=0
replace coeff_`y'=1 if timed==`y'
}

g alert=0
replace alert=1 if time>0 & export_service_EU==0
g zero=1
label var zero "-1"

forval y=0/22{
local j=`y'-11
label var coeff_`y' "`j'"
}

egen cluster=group(cae3 distrito)
save "$outputs/ES/paneldata_ES.dta", replace



*******************************************************
*. BASELINE EVENT-STUDIES (FIG8-9) 
*******************************************************


global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area

cap log close

log using "$outputs/ES/Baseline_Export5/logestimation.txt", replace


use "$outputs/ES/paneldata_ES.dta", clear
xtset tina ano
***********************

g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(1+MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(1+MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)
g loghourstemp=log(E018)
g loghourspart=log(E017)
g logssc=log(DL013)
g logtax=log(D060)
g logdebt=log(B081)
g alt=D001-MG005
g logalti=log(1+alt)
g logexpenses=log(DL045)
g logsalaries=log(D030)
g logcorporate=log(DL011)

g logexpenses_cap=log(DL045/E012)
g logsalaries_cap=log(D030/E012)
g logcorporate_cap=log(DL011/E012)

g expenses=DL045-DL013-DL014
g expenses_cap=log(expenses/E012)
g supp=log(DL043/E012)
g expenses_cap_bis=log(1+expenses/E012)
g supp_big=log(1+DL043/E012)
g wage_cap_noexp=(D030+DL013)/E012
g logwage_cap_noexp=log(wage_cap_noexp)
g logcost=log(D085)


*drop minor number of firms that export to other EU market (mode IV but no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1

g share_service=MG002/MG011
drop cluster

egen cluster1=group(ano distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)

*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4
label var coeff_up ">6"

*Keep only treated firms where we observe at least 4 years (2006-2010) before treatment without treatment
*means we must drop firms observed posting workers in the first four years of the sample
*Similarly, drop firms that started posting workers after 2015, to observe at least 2 years post treatment 

drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
   tab coeff_bottom coeff_7 if treated==1
   tab coeff_up2 coeff_15 if treated==1 
   
   
   *************************************************************
   * Version in appendix: using non-using firms as control group
   *************************************************************

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'.png", replace
}
}


foreach x in logcash logtangible logdebt loghourstemp loghourspart logssc logtax{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'.png", replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'_cut.png", replace
}
}



foreach x in share_service logwage_cap_noexp{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'_cut.png", replace
}
}

foreach x in logturnoverinternal loginternal logalti{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(cluster`y')
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'_cut.png", replace
}
}


**************************************************************************
* Baseline specification: pure event-studies, keeping only treated firms 
***************************************************************************


keep if treated==1
egen clusterb1=group(time distrito)
g post=0

*******************************
* Figure 8-9; 
******************************

foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'_cut.png", replace
}
}

foreach x in logexpenses logsalaries logcorporate logexpenses_cap logsalaries_cap logcorporate_cap{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'_cut.png", replace
}
}


*Appendix E 

foreach x in logcash logtangible logdebt loghourstemp loghourspart logssc logtax{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'_cut.png", replace
}
}


foreach x in logwage {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if wage_cap<1000, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_ot_c`y'nooutlier.txt",replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'_cutnooutlier.png", replace
}
}

foreach x in logturnoverinternal loginternal logalti{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_ot_c`y'.txt",replace

coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-1 (.4) .4) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'_cut.png", replace
}
}


reghdfe logtangible coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/logtangible_ot.txt",replace
est store tangible

reghdfe logcash coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/logcash_ot.txt",replace
est store cash




coefplot  (cash, label("Cash Assets")) (tangible, label("Fixed Assets") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1))
graph export "$outputs/ES/Baseline_Export5/Figures/assets_ot_decompo_cut.png", replace


reghdfe expenses_cap coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
est store expenses

reghdfe logsalaries_cap coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb1)
est store salaries




coefplot  (expenses, label("Expenses")) (salaries, label("Salaries") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1))
graph export "$outputs/ES/Baseline_Export5/Figures/wage_ot_decompo_cut.png", replace


reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb)
est store wage

reghdfe logEBT coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cluster(clusterb)
est store profit


coefplot  (wage, label("wage")) (profit, label("Profit") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1))



g logsalesEU=log(MG004)
g logservicesEUb=log(1+MG005)



*Appendix Figure of TFP 

g loginputs=log(D025)


reghdfe logca coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 logemp logassets loginputs, absorb(tina i.ano##i.distrito##i.cae3) cluster(clusterb1)
est store tfpols
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)
graph export "$outputs/ES/Baseline_Export5/Figures/tfpOLS_ot_cut.png", replace

acfest logca, free(logemp) state(cae3) proxy(loginputs) i(tina) t(ano)
predict myomega, omega 
reghdfe myomega coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 , absorb(tina i.ano##i.distrito##i.cae3) cluster(clusterb1)
est store acf

*Figure E.LVI

coefplot  (tfpols, label("TFP, OLS")) (acf, label("TFP, ACF") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1))
graph export "$outputs/ES/Baseline_Export5/Figures/tfpeffects.png", replace


*Appendix figures, asinh robustness 

g arcwage_cap=asinh(wage_cap)
g arcEBT=asinh(D086)
g arcemp=asinh(E001)
g arcca=asinh(D001)
g arcwagebill=asinh(D029)
g logwage2=log(1+wage_cap)
g logEBT2=log(1+D086)
g logca2=log(1+D001)
g logemp2=log(1+E001)
g logwagebill2=log(1+D029)

*Figure E.LXVI

foreach x in arcwage_cap arcEBT arcemp arcca arcwagebill logwage2 logEBT2 logca2 logemp2 logwagebill2 {
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 , absorb( i.tina  i.ano##i.distrito##i.cae3) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Estimates/`x'_ot_c`y'.txt",replace
coefplot, keep( coeff_bottom coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10))  ///
xline(5.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'.png", replace
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10))  ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Figures/`x'_ot_c`y'_cut.png", replace
}
}




g logssccap=log(DL013/E001)
reghdfe logssccap coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 , absorb( i.tina  i.ano##i.distrito##i.cae3) cluster(clusterb1)
coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10))  ///
xline(4.5, lcolor(red)) scale(1.2) ylabel(-.6 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Figures/logssccap_ot_c1_cut.png", replace

*Robustness to winsorizing wage per capita 

winsor wage_cap, p(0.01) g(wage_w)
g logwage_w=log(wage_w)
reghdfe logwage_w coeff_bottom coeff_5-coeff_9 zero coeff_11-coeff_15 coeff_up2 , absorb( i.tina  i.ano##i.distrito##i.cae3) cluster(clusterb1)

*Using the destination-specific minimum wage index computed outside the server for Portuguese firms using other datasets on posting 

g binded=0
replace binded=1 if wage_cap<7.32 & time<0 
egen b=max(binded), by(tina)


reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if b==1, absorb( i.tina  i.ano##i.distrito##i.cae3) cluster(clusterb1)
est store binded
reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if b==0 , absorb( i.tina  i.ano##i.distrito##i.cae3) cluster(clusterb1)
est store notbinded

*Figure E.LXXI

coefplot  (binded, label("Binding Minimum Wage")) (notbinded, label("Non Binding Minimum Wage") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) recast(connected) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1))
graph export "$outputs/ES/Baseline_Export5/Figures/binded.png", replace












